using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace cb0tServer
{
    class MOTD
    {
        private static List<String> lines = new List<String>();

        public static void LoadMotd()
        {
            lines.Clear();

            if (File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "motd.txt"))
            {
                try
                {
                    String[] temp = File.ReadAllLines(sb0t_winGUI.RunOnceFileUpdater.sbotpath +
                        "motd.txt", Encoding.UTF8);

                    if (temp.Length > 0)
                        lines = new List<String>(temp);

                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket("14MOTD Loaded [" +
                        Encoding.UTF8.GetByteCount(String.Join("\r\n", temp)) + " bytes]"));
                }
                catch { }
            }
            else // default
            {
                lines.Add("304Welcome to my room +n :-)");
                lines.Add("303Have fun!");
                File.WriteAllLines(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "motd.txt",
                    lines.ToArray(), Encoding.UTF8);
            }
        }

        public static void Init()
        {
            if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "motd.txt"))
            {
                lines.Add("304Welcome to my room +n :-)");
                lines.Add("303Have fun!");
                File.WriteAllLines(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "motd.txt",
                    lines.ToArray(), Encoding.UTF8);
            }
        }

        public static void ViewMotd(UserObject userobj)
        {
            if (lines.Count == 0) return;

            UserObject u = UserPool.RandomUser();
            String rnd_user = u == null ? "" : u.name;

            String _str = Helpers.CountryCodeToString(userobj.user_country);

            if (_str != "?")
                if (userobj.user_region.Length > 0)
                    _str = userobj.user_region + ", " + _str;

            if (_str == "?")
                if (userobj.user_region.Length > 0)
                    _str = userobj.user_region;

            if (_str == "?")
                _str = "unknown";

            if (userobj.supports_compression)
            {
                List<byte> accumulator = new List<byte>();

                foreach (String line in lines)
                {
                    String str = line;
                    str = str.Replace("+n", userobj.name);
                    str = str.Replace("+ip", userobj.physical_ip.ToString());
                    str = str.Replace("+id", userobj.id.ToString());
                    str = str.Replace("+f", userobj.files.ToString());
                    str = str.Replace("+v", userobj.version);
                    str = str.Replace("+p", userobj.dc_port.ToString());
                    str = str.Replace("+uc", UserPool.UserCount().ToString());
                    str = str.Replace("+rn", CoreSettings.room_name);
                    str = str.Replace("+ut", Stats.GetUptime());
                    str = str.Replace("+ru", rnd_user);
                    str = str.Replace("+l", _str);
                    
                    byte[] packet = ServerOutboundPackets.AnnoucePacket(Helpers.SetColors(str));
                    
                    accumulator.AddRange(packet); // add line to accumulator

                    if (accumulator.Count > 800) // let's compress and send
                    {
                        packet = ServerOutboundPackets.CompressPacket(accumulator.ToArray());
                        accumulator.Clear();
                        userobj.SendOutboundPacket(packet);
                    }
                }

                if (accumulator.Count > 0) // residual
                {
                    byte[] packet = ServerOutboundPackets.CompressPacket(accumulator.ToArray());
                    accumulator.Clear();
                    userobj.SendOutboundPacket(packet);
                }
            }
            else
            {
                foreach (String line in lines)
                {
                    String str = line;
                    str = str.Replace("+n", userobj.name);
                    str = str.Replace("+ip", userobj.physical_ip.ToString());
                    str = str.Replace("+id", userobj.id.ToString());
                    str = str.Replace("+f", userobj.files.ToString());
                    str = str.Replace("+v", userobj.version);
                    str = str.Replace("+p", userobj.dc_port.ToString());
                    str = str.Replace("+uc", UserPool.UserCount().ToString());
                    str = str.Replace("+rn", CoreSettings.room_name);
                    str = str.Replace("+ut", Stats.GetUptime());
                    str = str.Replace("+ru", rnd_user);
                    str = str.Replace("+l", _str);
                    userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(Helpers.SetColors(str)));
                }
            }
        }

    }
}
